package com.sen.rbac.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sen.common.common.controller.BaseController;
import com.sen.common.common.exception.RecordNotFoundException;
import com.sen.common.common.response.MessageResult;
import com.sen.common.common.response.PageVo;
import com.sen.common.common.util.CheckUtils;
import com.sen.rbac.dto.SysTenantDTO;
import com.sen.rbac.dto.SysTenantResourceDTO;
import com.sen.rbac.feign.RbacTenantFeign;
import com.sen.rbac.model.SysTenant;
import com.sen.rbac.service.SysTenantService;
import com.sen.rbac.vo.SysTenantVO;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.sen.common.common.response.ResultMsgEnum.RECORD_NOT_EXIST;

/**
 * @author esjiang
 */
@RestController
@RequestMapping("/tenant")
public class TenantController extends BaseController implements RbacTenantFeign {

    @Resource
    SysTenantService sysTenantService ;

    @Resource
    ModelMapper modelMapper ;


    @PostMapping("/list")
    public MessageResult list(@RequestBody SysTenantDTO sysTenantDTO){
        return exec("查询租户列表",sysTenantDTO,()->{
            CheckUtils.checkNotNull(sysTenantDTO);
            PageVo<SysTenantVO> list = sysTenantService.list(sysTenantDTO);
            return MessageResult.success(list) ;
        }) ;
    }

    @PostMapping("/saveOrUpdate")
    public MessageResult saveOrUpdate(@RequestBody SysTenantDTO sysTenantDTO){
        return exec("保存或修改",sysTenantDTO,()->{
            if (ObjectUtil.isNull(sysTenantDTO.getId())){
                CheckUtils.checkNotNull(sysTenantDTO,"tenantName","area","administrator","password");
            }
            sysTenantService.saveOrUpdate(sysTenantDTO);
            return MessageResult.success() ;
        }) ;
    }

    @GetMapping("/get")
    @Override
    public MessageResult<SysTenantVO> get(@RequestParam String tenantCode){
        return exec("详情查询",tenantCode,()->{
            CheckUtils.checkNotNull(tenantCode);
            SysTenant sysTenant = sysTenantService.getOne(Wrappers.<SysTenant>lambdaQuery().eq(SysTenant::getTenantCode, tenantCode));
            if (ObjectUtil.isNull(sysTenant)){
                throw new RecordNotFoundException(RECORD_NOT_EXIST) ;
            }
            return MessageResult.success(modelMapper.map(sysTenant,SysTenantVO.class)) ;
        }) ;
    }

    @GetMapping("/del")
    public MessageResult del(@RequestParam String id){
        return exec("删除租户",id,()->{
            CheckUtils.checkNotNull(id);
            sysTenantService.del(id);
            return MessageResult.success() ;
        });
    }

    @PostMapping("/related")
    public MessageResult relatedResource(@RequestBody SysTenantResourceDTO sysTenantResourceDTO){
        return exec("分配资源",sysTenantResourceDTO,()->{
            CheckUtils.checkNotNull(sysTenantResourceDTO,"tenantCode");
            sysTenantService.relatedResource(sysTenantResourceDTO);
            return  MessageResult.success() ;
        });
    }

    @GetMapping(value = "/getTenantList")
    @Override
    public MessageResult<List<SysTenantVO>> getTenantList() {
        return exec("查询所有租户",null,()-> MessageResult.success(sysTenantService.listTenants())) ;
    }

}
